package org.summer.lawyer.controller.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.core.validate.AddGroup;
import org.summer.common.core.validate.EditGroup;
import org.summer.common.excel.utils.ExcelUtil;
import org.summer.common.idempotent.annotation.RepeatSubmit;
import org.summer.common.log.annotation.Log;
import org.summer.common.log.enums.BusinessType;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.web.core.BaseController;
import org.summer.lawyer.domain.bo.spucomment.SpuCommentBo;
import org.summer.lawyer.domain.vo.spucomment.SpuCommentVo;
import org.summer.lawyer.service.ISpuCommentService;

import java.util.List;

/**
 * 商品评价信息
 *
 * @author summer
 * @date 2024-06-21
 */
@Tag(name = "后台相关接口", description = "商品评价信息")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lawyer/spuComment")
public class SpuCommentController extends BaseController {

    private final ISpuCommentService spuCommentService;


    /**
     * 查询商品评价信息列表
     */
    @Operation(summary = "查询商品评价信息列表", description = "查询商品评价信息列表", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("trade:comment:list")
    @GetMapping("/list")
    public TableDataInfo<SpuCommentVo> list(SpuCommentBo bo, PageQuery pageQuery) {
        return spuCommentService.selectPageList(bo, pageQuery);
    }

    @Operation(summary = "查询律所评价接口")
    @SaCheckLogin
    @GetMapping("/listLawFirmComment")
    public TableDataInfo<SpuCommentVo> listLawFirmComment(SpuCommentBo bo, PageQuery pageQuery) {
        if (bo.getLawFirmId() == null) {
            bo.setLawFirmId(0L);
        }
        return spuCommentService.selectPageList(bo, pageQuery);
    }

    /**
     * 导出商品评价信息列表
     */
    @Operation(summary = "导出商品评价信息列表", description = "导出商品评价信息列表")
    @SaCheckPermission("trade:comment:export")
    @Log(title = "商品评价信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SpuCommentBo bo, HttpServletResponse response) {
        List<SpuCommentVo> list = spuCommentService.selectSpuCommentVoList(bo);
        ExcelUtil.exportExcel(list, "商品评价信息", SpuCommentVo.class, response);
    }

    /**
     * 获取商品评价信息详细信息
     *
     * @param id 主键
     */
    @Operation(summary = "查询商品评价信息列表", description = "查询商品评价信息列表", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("trade:comment:query")
    @GetMapping("/{id}")
    public R<SpuCommentVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(spuCommentService.selectSpuCommentVoById(id));
    }

    /**
     * 新增商品评价信息
     */
    @Operation(summary = "新增商品评价信息", description = "新增商品评价信息", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:comment:add")
    @Log(title = "商品评价信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/saveSpuComment")
    public R<Void> saveSpuComment(@Validated(AddGroup.class) @RequestBody SpuCommentBo bo) {
        return toAjax(spuCommentService.insertByBo(bo));
    }

    /**
     * 修改商品评价信息
     */
    @Operation(summary = "修改商品评价信息", description = "修改商品评价信息", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("lawyer:comment:edit")
    @Log(title = "商品评价信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/updateSpuComment")
    public R<Void> updateSpuComment(@Validated(EditGroup.class) @RequestBody SpuCommentBo bo) {
        return toAjax(spuCommentService.updateByBo(bo));
    }

    /**
     * 删除商品评价信息
     *
     * @param ids 主键串
     */
    @Operation(summary = "删除商品评价信息", description = "删除商品评价信息", responses = {@ApiResponse(responseCode = "200", description = "成功")})
    @SaCheckPermission("trade:comment:remove")
    @Log(title = "删除商品评价信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeSpuComment/{ids}")
    public R<Void> removeSpuComment(@NotEmpty(message = "主键不能为空")
                                    @PathVariable Long[] ids) {
        return toAjax(spuCommentService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     *
     */
    @SaCheckPermission("trade:comment:pass")
    @Log(title = "通过商品评价信息", businessType = BusinessType.UPDATE)
    @PostMapping("/passedSpuComment")
    public R<Void> passedSpuComment(@Validated @RequestBody SpuCommentBo bo){
        spuCommentService.passedSpuComment(bo);
        return R.ok();
    }

    @SaIgnore
    @GetMapping("/calRating/{lawFirmId}")
    public R<Void> calRating(@PathVariable("lawFirmId")Long lawFirmId){
        spuCommentService.calRating(lawFirmId);
        return R.ok();
    }
}
